using System.Collections.Generic;
using System.IO.IsolatedStorage;
using UltraLight.MVVM;
using UltraLight.MVVM.Contracts;

namespace UltraLight
{
    /// <summary>
    ///     Main view model
    /// </summary>
    public class MainViewModel : BaseViewModel, IMainViewModel, IEventSink<string>, ITombstoneFriendly
    {
        /// <summary>
        ///     Hold the last view state
        /// </summary>
        private string _lastViewState = string.Empty;

        /// <summary>
        ///     Set it all up
        /// </summary>
        public MainViewModel()
        {
            UltraLightLocator.EventAggregator.SubscribeOnDispatcher(this);
            
            EventCommand = new ActionCommand<object>(
                o => UltraLightLocator.EventAggregator.Publish(Text),
                o=>!string.IsNullOrEmpty(Text));
            
            DialogCommand = new ActionCommand<object>(
                o => _RequestDelete(),
                o => !string.IsNullOrEmpty(Text));
            
            StateCommand = new ActionCommand<object>(
                s =>
                    {
                        GoToVisualState(s.ToString(), true);
                        _lastViewState = s.ToString();
                    },
                s=>s is string && !string.IsNullOrEmpty((string)s));
            
            NavigateCommand = new ActionCommand<object>(o => _Navigate());   
            
            MenuCommand = new ActionCommand<object>(
                o => Dialog.ShowMessage("Menu","Menu Item 1 Clicked",false));
        }

        /// <summary>
        ///     For pages, the list of commands to bind to application bar buttons
        /// </summary>
        public override IEnumerable<IActionCommand<object>> ApplicationButtonBindings
        {
            get { return new[] { DialogCommand }; }
        }

        /// <summary>
        ///     For pages, the list of commands to bind to application bar menu items
        /// </summary>
        public override IEnumerable<IActionCommand<object>> ApplicationMenuBindings
        {
            get { return new[] { MenuCommand }; }
        }

        private string _text;

        /// <summary>
        ///     Sample text
        /// </summary>
        public string Text
        {
            get { return _text; }
            set
            {
                _text = value;
                RaisePropertyChanged(() => Text);
                EventCommand.RaiseCanExecuteChanged();
                DialogCommand.RaiseCanExecuteChanged();
            }
        }

        /// <summary>
        ///     Send an event/message
        /// </summary>
        public IActionCommand<object> EventCommand { get; private set; }

        /// <summary>
        ///     Change the state
        /// </summary>
        public IActionCommand<object> StateCommand { get; private set; }

        /// <summary>
        ///     Pop up the dialog 
        /// </summary>
        public IActionCommand<object> DialogCommand { get; private set; }

        /// <summary>
        ///     Navigate
        /// </summary>
        public IActionCommand<object> NavigateCommand { get; private set; }

        /// <summary>
        /// Menu item
        /// </summary>
        public IActionCommand<object> MenuCommand { get; private set; }

        /// <summary>
        ///     Navigate to the second page
        /// </summary>
        private void _Navigate()
        {
            RequestNavigation("/SecondPage.xaml");
        }

        /// <summary>
        ///     Ask to clear the text
        /// </summary>
        private void _RequestDelete()
        {
            if (Dialog.ShowMessage("Confirm Delete","Click OK to Clear the Text.", true))
            {
                Text = string.Empty;
            }
        }

        public override bool CancelBackRequest()
        {
            return !Dialog.ShowMessage("Close Application", "Are you sure you wish to leave?", true);
        }
                
        /// <summary>
        ///     Handle the published string event
        /// </summary>
        /// <param name="publishedEvent">The text that was sent</param>
        public void HandleEvent(string publishedEvent)
        {
            Dialog.ShowMessage("Received Text", publishedEvent, false);
        }

        /// <summary>
        ///     Tombstone
        /// </summary>
        public void Deactivate()
        {
            var settings = IsolatedStorageSettings.ApplicationSettings;
            settings["ViewState"] = _lastViewState;
            settings["Text"] = Text;
        }

        /// <summary>
        ///     Returned from tombstone
        /// </summary>
        public void Activate()
        {
            var settings = IsolatedStorageSettings.ApplicationSettings;
            if (settings.Contains("ViewState"))
            {
                _lastViewState = (string) settings["ViewState"];
                GoToVisualState(_lastViewState, false);
            }
            if (settings.Contains("Text"))
            {
                Text = (string) settings["Text"];
            }
        }
    }
}